package abc.com.abc.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import abc.com.abc.R;
import abc.com.abc.bean.UserBean;
import abc.com.abc.utils.AnalysisiUtils;
import abc.com.abc.utils.DBUtils;

public class UserInfoActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView tv_back;
    private TextView tv_main_title,tv_name,tv_jiakao,tv_jiaolian,tv_phone,tv_username,tv_carModel;
    private RelativeLayout rl_username,rl_name,rl_jiakao,rl_jiaolian,rl_phone,rl_title_bar,rl_carModel;
    private String spUserName;
    private static final int CHANGE_NAME = 1; // 修改name的自定义常量
    private static final int CHANGE_JIAOLIAN = 2; // 修改教练名的自定义常量
    private static final int CHANGE_PHONE = 3;//修改电话的自定义常量
    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        spUserName = AnalysisiUtils.readLoginUserName(this);
        init();
        initData();
        setListener();
    }

    /**
     * 设置监听
     */
    private void setListener() {
        tv_back.setOnClickListener(this);
        rl_name.setOnClickListener(this);
        rl_jiakao.setOnClickListener(this);
        rl_jiaolian.setOnClickListener(this);
        rl_phone.setOnClickListener(this);
        rl_carModel.setOnClickListener(this);
    }

    /**
     * 获取数据
     */
    private void initData() {
        UserBean bean = null;
        bean = DBUtils.getInstance(this).getUserInfo(spUserName);
        //首先判断是否有数据
        if (bean == null){
            bean = new UserBean();
            bean.userName = spUserName;
            bean.name = "张三";
            bean.carModel = "c1";
            bean.jiakao = "科目一";
            bean.jiaolian = "张教练";
            bean.phone = "15243687576";
            //保存到数据库
            DBUtils.getInstance(this).saveUserInfo(bean);
        }
        setValue(bean);
        
    }
//为界面控件设置值
    private void setValue(UserBean bean) {
        tv_name.setText(bean.name);
        tv_username.setText(bean.userName);
        tv_carModel.setText(bean.carModel);
        tv_jiakao.setText(bean.jiakao);
        tv_jiaolian.setText(bean.jiaolian);
        tv_phone.setText(bean.phone);


    }

    /**
     * 初始化控件
     */
    private void init() {
        tv_back = findViewById(R.id.tv_back);
        tv_main_title = findViewById(R.id.tv_main_title);
        tv_main_title.setText("个人资料");
        rl_title_bar = findViewById(R.id.title_Bar);
        rl_title_bar.setBackgroundColor(Color.parseColor("#30b4ff"));
        rl_name = findViewById(R.id.rl_name);
        rl_jiakao = findViewById(R.id.rl_jiakao);
        rl_jiaolian = findViewById(R.id.rl_jiaolian);
        rl_carModel = findViewById(R.id.rl_carModel);
        rl_phone = findViewById(R.id.rl_phone);
        tv_name=findViewById(R.id.tv_name);
        tv_username = findViewById(R.id.tv_user_name);
        tv_jiakao = findViewById(R.id.tv_jiakao);
        tv_jiaolian = findViewById(R.id.tv_jiaolian);
        tv_phone = findViewById(R.id.phone);
        tv_carModel = findViewById(R.id.tv_carModel);



    }
/*
控件点击事件
 */
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case  R.id.tv_back://返回键的点击
                this.finish();
                break;
            case  R.id.rl_name:// 姓名点击
                String name = tv_name.getText().toString();//获取姓名控件上的数据
                Bundle bdName = new Bundle();
                bdName.putString("content",name);//传递界面上的姓名数据
                bdName.putString("title","姓名");
                bdName.putInt("flag",CHANGE_NAME); //flag传递1时是修改姓名

                enterActivityForResult(ChangeUserInfoActivity.class,CHANGE_NAME,bdName);//跳转
                break;
            case R.id.rl_carModel://车型点击

                String carModel = tv_carModel.getText().toString();
                carModelDialog(carModel);

                Log.i("dd", "onClick: carmodel"+carModel);

                break;
            case R.id.rl_jiakao://科目几的点击
               String jiakao = tv_jiakao.getText().toString();
                jiakaoDialog(jiakao);
                break;
            case  R.id.rl_jiaolian://教练的点击
                String jiaolian = tv_jiaolian.getText().toString();
                Bundle bdjiaolian = new Bundle();
                bdjiaolian.putString("content",jiaolian);
                bdjiaolian.putString("title","教练名");
                bdjiaolian.putInt("flag",CHANGE_JIAOLIAN);
                enterActivityForResult(ChangeUserInfoActivity.class,CHANGE_JIAOLIAN,bdjiaolian);//跳转

                break;
            case R.id.rl_phone://电话的点击
                String phone = tv_phone.getText().toString();
                Bundle bdphone = new Bundle();
                bdphone.putString("content",phone);
                bdphone.putString("title","电话");
                bdphone.putInt("flag",CHANGE_PHONE);
                enterActivityForResult(ChangeUserInfoActivity.class,CHANGE_PHONE,bdphone);//跳转

                break;
                default:
                    break;
        }
        
    }
//设置车型的弹出框
    private void carModelDialog(String carModel) {
        int carModelFlag = 0;
       if ("C1 ".equals(carModel)){
           carModelFlag = 0;
       }else if ("C2".equals(carModel)){
            carModelFlag =1;
       }
      final String items[]={"C1","C2"};
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("车型");
        builder.setSingleChoiceItems(items, 1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
                Toast.makeText(UserInfoActivity.this,items[i],Toast.LENGTH_SHORT).show();
                setCarModel(items[i]);//更新界面的车型数据
            }
        });
        builder.show();
    }

    //更新界面的车型数据
    private void setCarModel(String carModel) {
        tv_carModel.setText(carModel);
        DBUtils.getInstance(UserInfoActivity.this).updateUserInfo("carModel",carModel,spUserName);
    }

    private void jiakaoDialog(String jiakao) {
        int jiakaoFlag = 0;
        if ("科目一".equals(jiakao)){
            jiakaoFlag = 0;
        }else  if ("科目二".equals(jiakao)){
            jiakaoFlag = 1;
        }else if ("科目三".equals(jiakao)){
            jiakaoFlag =2;
        }else if ("科目四".equals(jiakao)){
            jiakaoFlag =3;
        }
        final String items[]={"科目一","科目二","科目三","科目四"};
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("正在考科目几？");
        builder.setSingleChoiceItems(items, jiakaoFlag, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                Toast.makeText(UserInfoActivity.this,items[which],Toast.LENGTH_SHORT).show();
                setJiakao(items[which]);
            }
        });
        builder.show();
    }
/*更新驾考科目几的页面*/
    private void setJiakao(String jiakao) {
        tv_jiakao.setText(jiakao);
        DBUtils.getInstance(UserInfoActivity.this).updateUserInfo("jiakao",jiakao,spUserName);
    }
    //页面跳转
    public void enterActivityForResult(Class<?> to,int requestCode,Bundle b){
        Intent intent = new Intent(this,to);
         intent.putExtras(b);
        startActivityForResult(intent,requestCode);
    }

    @Override
    //将接收到的新数据传到数据库中
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode,resultCode,data);
        switch (requestCode){
            case CHANGE_NAME :
                if (data != null){
                    String new_info = data.getStringExtra("name");
                    if (TextUtils.isEmpty(new_info)){
                        return;
                    }
                    tv_name.setText(new_info);
                    //update
                    DBUtils.getInstance(UserInfoActivity.this).updateUserInfo("name"
                    ,new_info,spUserName);
                }
                break;
            case CHANGE_JIAOLIAN:
                if (data != null){
                    String new_info = data.getStringExtra("jiaolian");
                    if (TextUtils.isEmpty(new_info)){
                        return;
                    }
                    tv_jiaolian.setText(new_info);
                    //update
                    DBUtils.getInstance(UserInfoActivity.this).updateUserInfo("jiaolian"
                            ,new_info,spUserName);
                }
                break;
            case CHANGE_PHONE:
                if (data != null){
                    String new_info = data.getStringExtra("phone");
                    if (TextUtils.isEmpty(new_info)){
                        return;
                    }
                    tv_phone.setText(new_info);
                    //update
                    DBUtils.getInstance(UserInfoActivity.this).updateUserInfo("phone"
                            ,new_info,spUserName);
                }
                break;
        }
    }
}
